const preventSystemBackModal = uni.requireNativePlugin('HG-PreventBack');
import { setStorage } from '@/utils/setStorage.js';
const saoma = async (cd) => {
	// setStorage('saomaType', 'on')
	setStorage('saomaType', 'off')
	const isAndroid = uni.getSystemInfoSync().platform
	switch (isAndroid) {
		case 'android':
			preventSystemBackModal.setUp({
				enable: false //是否拦截系统返回功能
			});
			break;
	}
	uni.scanCode({
		onlyFromCamera: true,
		success: function(res) {
			plus.device.vibrate(500); // 震动
			const innerAudioContext = uni.createInnerAudioContext();
			innerAudioContext.autoplay = true;
			innerAudioContext.src = '/static/video/saoma.mp3'; //音频地址
			innerAudioContext.onPlay();
			cd(res)
			opens(isAndroid)
		},
		fail(err) {
			opens(isAndroid)
		}
	});
	if (await judgeIosPermissionCamera()) {}
}
// 判断相机权限是否开启j
const judgeIosPermissionCamera = () => {
	return new Promise((resolve, reject) => {
		var isIos = plus.os.name == 'iOS';
		if (isIos) {
			var AVCaptureDevice = plus.ios.import('AVCaptureDevice');
			var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
			if (authStatus == 3) {
				resolve(true);
			} else {
				uni.showModal({
					title: 'Permission warning',
					content: 'To access the scanning function, you need to allow the camera permission function',
					confirmText: 'Go to open',
					cancelText: 'cancel',
					success: function(res) {
						if (res.confirm) {
							var UIApplication = plus.ios.import('UIApplication');
							var application2 = UIApplication.sharedApplication();
							var NSURL2 = plus.ios.import('NSURL');
							// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
							var setting2 = NSURL2.URLWithString('app-settings:');
							application2.openURL(setting2);

							plus.ios.deleteObject(setting2);
							plus.ios.deleteObject(NSURL2);
						}
					}
				});
			}
			plus.ios.deleteObject(AVCaptureDevice);
			return result;
		} else {
			plus.android.requestPermissions(
				['android.permission.CAMERA'],
				function(e) {
					if (e.deniedAlways.length > 0) {
						//权限被永久拒绝
						// 弹出提示框解释为何需要权限，引导用户打开设置页面开启
						uni.showModal({
							title: 'Permission warning',
							content: 'To access the scanning function, you need to allow the camera permission function',
							confirmText: 'Go to open',
							cancelText: 'cancel',
							success: function(res) {
								if (res.confirm) {
									var Intent = plus.android.importClass(
										'android.content.Intent');
									var Settings = plus.android.importClass(
										'android.provider.Settings');
									var Uri = plus.android.importClass('android.net.Uri');
									var mainActivity = plus.android.runtimeMainActivity();
									var intent = new Intent();
									intent.setAction(Settings
										.ACTION_APPLICATION_DETAILS_SETTINGS);
									var uri = Uri.fromParts('package', mainActivity
										.getPackageName(), null);
									intent.setData(uri);
									mainActivity.startActivity(intent);
									setStorage('saomaType', 'off')
								} else {
									// opens('android')
									preventSystemBackModal.setUp({
										enable: false //是否拦截系统返回功能
									});
								}
							}
						});
					}
					if (e.deniedPresent.length > 0) {
						resolve(true);
						//权限被临时拒绝
						// 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限
						// console.log('权限被临时拒绝' + e.deniedPresent.toString());
					}
					if (e.granted.length > 0) {
						resolve(true);
						//权限被允许
					}
				},
				function(e) {
					uni.showToast({
						title: JSON.stringify(e),
						icon: 'none',
						position: 'top'
					});
				}
			);
		}
	});
}
const opens = (isAndroid) => {
	if (isAndroid == 'android') {
		preventSystemBackModal.setUp({
				enable: true //是否拦截系统返回功能
			},
			ret => {
				uni.$emit('goBack_k', true);
			}
		);
		setStorage('saomaType', 'on')
	}

}
export default saoma
